
package com.ukao.cashregister.pesenter;

import com.ukao.cashregister.base.BasePresenter;
import com.ukao.cashregister.bean.BalanceBean;
import com.ukao.cashregister.bean.StringBean;
import com.ukao.cashregister.bean.UserInfoBean;
import com.ukao.cashregister.consts.Constant;
import com.ukao.cashregister.consts.SaveParamets;
import com.ukao.cashregister.retrofit.ApiCallback;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.DecimalUtil;
import com.ukao.cashregister.utils.L;
import com.ukao.cashregister.utils.Utils;
import com.ukao.cashregister.view.QuiltSweepView;

import java.util.HashMap;

import rx.Observable;

import static com.ukao.cashregister.R.id.balance;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class QuiltSweepPesenter extends BasePresenter<QuiltSweepView> {

    public QuiltSweepPesenter(QuiltSweepView view) {
        attachView(view);
    }


    /**
     * 二维码 支付
     * @param orderId
     * @param balance
     * @param isWeChat  是否为微信
     * @param isUseUPay  是否为使用优付卡
     * @param cpnRelId    优惠券ID
     */
    public void wxUnifiedOrder (String orderId,String  balance,boolean  isWeChat,boolean  isUseUPay,String  cpnRelId ) {
       mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",orderId);
        if(isUseUPay){
            mHashMap.put("groupCardType","41");//groupCardType 支付(团)卡类型 41-优付卡
        }else{
            mHashMap.put("balance",balance);
        }
        if (!CheckUtils.isEmpty(cpnRelId)){
            mHashMap.put("cpnRelId", cpnRelId);
        }
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        Observable mobservable;
        if (isWeChat){
            mobservable = apiStores.wxUnifiedOrder(Constant.createParameter(mHashMap));

        }else{
            mobservable= apiStores.aliUnifiedOrder(Constant.createParameter(mHashMap));
        }

        L.i("mHashMap="+mHashMap.toString());
        addSubscription(mobservable,
                new ApiCallback<BalanceBean>() {
                    @Override
                    public void onSuccess(BalanceBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadQRCodeData(model.getData());
                        }else{
                            mvpView.scanPayMicroFail(model.getMsg());
                        }

                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }
    /**
     查询交易状态
     */
    public void queryStatusBySeq (String seq  ) {
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("seq", seq);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.queryStatusBySeq(Constant.createParameter(mHashMap)),
                new ApiCallback<BalanceBean>() {
                    @Override
                    public void onSuccess(BalanceBean model) {
                        if (model.getHttpCode() == 200) {
                            mvpView.queryStatusData(model.getData());
                        } else {
                            mvpView.loadFail(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {
                    }

                });


    }


    /*支付解锁*/
    public void unlockOrder(String orderId    ) {
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId", orderId);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.unlockOrder(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode() == 200) {

                            mvpView.unlockOrderData(  );

                        } else {

                            mvpView.unlockOrderFail();
                        }

                    }


                    @Override
                    public void onFinish() {

                    }

                });
    }

    /**
     * 微信刷卡支付-主扫
     * @param orderId 订单ID
     * @param balance  使用的多少余额
     * @param paymentCode  二维码
     * @param cpnRelId    优惠券ID
     */
    public void wxMicroPay (String orderId,String  balance,String  paymentCode,boolean  isWeChat,boolean  isUseUPay,String  cpnRelId) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",orderId  );
        mHashMap.put("authCode",paymentCode);
        if (!CheckUtils.isEmpty(cpnRelId)){
            mHashMap.put("cpnRelId", cpnRelId);
        }
        if(isUseUPay){
            mHashMap.put("groupCardType","41");//groupCardType 支付(团)卡类型 41-优付卡
        }else{
            mHashMap.put("balance",balance);
        }
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        Observable  mobservable;
        if (isWeChat){
            mobservable = apiStores.wxMicroPay(Constant.createParameter(mHashMap));
        }else{
            mobservable = apiStores.aliMicroPay(Constant.createParameter(mHashMap));
        }
        L.i("mHashMap="+mHashMap.toString());
        addSubscription(mobservable,
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadPayData(model.getMsg());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }

                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }
    /**  接口已经过时
     * 微信刷卡支付-主扫 充值
     * userId 用户id
     amount 充值金额（单位分）
     remark 备注
     authCode 微信授权码
     withActive 1.参数活动 0.不参数活动
     */
    public void wxMicroRecharge (String userId,String  amount,String  authCode,String  withActive,boolean  isWeChat) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("userId",userId);
        mHashMap.put("amount", DecimalUtil.multiply(amount));
        mHashMap.put("authCode",authCode);
        mHashMap.put("withActive",withActive);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        Observable  mobservable=null;
        if (isWeChat){
            mobservable = apiStores.wxMicroRecharge(Constant.createParameter(mHashMap));
        }else{
            mobservable = apiStores.aliMicroRecharge(Constant.createParameter(mHashMap));
        }
        L.i("mHashMap="+mHashMap.toString());
        addSubscription(mobservable,
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadPayData(model.getMsg());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }

                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     * 二维码 充值  被扫充值
     * @param userId
     * @param balance
     * @param isWeChat  是否为微信
     * @param withActive  1.参数活动 0.不参数活动
     */
    public void quiltRecharge (String userId ,String  balance,String  withActive,boolean  isWeChat ) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("amount",DecimalUtil.multiply(balance));
        mHashMap.put("userId",userId);
        mHashMap.put("withActive",withActive);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        Observable mobservable;
        if (isWeChat){
            mobservable= apiStores.wxRecharge(Constant.createParameter(mHashMap));
        }else{
            mobservable = apiStores.aliRecharge(Constant.createParameter(mHashMap));
        }
        L.i("mHashMap="+mHashMap.toString());
        addSubscription(mobservable,
                new ApiCallback<BalanceBean>() {
                    @Override
                    public void onSuccess(BalanceBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadQRCodeData(model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }

                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     *  充值主扫
     * @param userId  用户名id
     * @param amount  充值金额
     * @param withActive 是否参与充值活动
     * @param isWeChat  是否是微信
     * @param isWeChat  支付方式  1.微信 4.支付宝
     * @param isMasterScanner 是否是主扫  支付类型	NATIVE：二维码支付(被扫) MICROPAY：扫码支付(主扫)

     */
    public void wxAirectMicroRecharge (final  String authCode,String userId,String  amount  ,String  withActive,boolean  isWeChat  ,boolean  isMasterScanner) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("userId",userId);
        mHashMap.put("amount", DecimalUtil.multiply(amount));
        mHashMap.put("withActive",withActive);
        mHashMap.put("payMethod",isWeChat?"1":"4");
        mHashMap.put("payType",isMasterScanner?"MICROPAY":"NATIVE");
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        L.i("mHashMap="+mHashMap.toString());
        addSubscription( apiStores.createPay(Constant.createParameter(mHashMap)),
                new ApiCallback<BalanceBean>() {
                    @Override
                    public void onSuccess(BalanceBean model) {
                        if (model.getHttpCode()==200){

                           useRechargePay(model.getData().getTradeSeq(),authCode);
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }

                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     * 用户线上主扫充值-支付
     * @param tradeSeq
     * @param authCode
     */
    public void useRechargePay (final  String tradeSeq,String  authCode ) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("tradeSeq",tradeSeq);
        mHashMap.put("authCode",authCode);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        L.i("mHashMap="+mHashMap.toString());
        addSubscription( apiStores.userRechargePay(Constant.createParameter(mHashMap)),
                new ApiCallback<BalanceBean>() {
                    @Override
                    public void onSuccess(BalanceBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.userRechargeData(model.getData(),tradeSeq);
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }

                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     *用户信息
     */
    public void userinfoData (  String useid    ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("id",useid) ;
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.userInfoDetail(Constant.createParameter(mHashMap)),
                new ApiCallback<UserInfoBean.UserBean>() {
                    @Override
                    public void onSuccess(UserInfoBean.UserBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.userinfoSucced(model.getData());

                        }else{
                            mvpView.loadFail(model.getMsg());
                        }
                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();

                    }

                });
    }


}
